/**
  *场外玩家列表
  */
module game {

    export class UserListMediator extends BaseMediator {
        public static NAME: string = "UserListMediator";

        public constructor(viewComponent: any = null) {
            super(UserListMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_USER_LIST,
                PanelNotify.HALL.CLOSE_USER_LIST
            ];
        }
        private panel: UserListPanel;
        private effectType = 0;
        private gameType = "";
        public handleNotification(notification: puremvc.INotification): void {
            this.gameType = notification.getBody();
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_USER_LIST: {
                    //根据传过来的游戏类型决定窗口在左边还是右边
                    if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.BRNN) {
                        this.effectType = 4
                    }
                    if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.WJL) {
                        this.effectType = 4
                    }
                    if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.LHD) {
                        this.effectType = 3
                    }
                    if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.BREBG) {
                        this.effectType = 4
                    }
                    if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.HHDZ) {
                        this.effectType = 3
                    }
                    this.panel = null;
                    this.panel = new UserListPanel();
                    this.showUI(this.panel, 2, this.effectType, true, true);
                    break;
                }
                case PanelNotify.HALL.CLOSE_USER_LIST: {
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this);
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            //根据传过来的游戏类型查询对应的玩家列表 this.gameType
            var userArr: Array<Object>;
            if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.BRNN) {
                userArr = BrnnVO.getVo().structUserList
            }
            if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.WJL) {
                userArr = WjlVO.getVo().structUserList
            }
            if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.LHD) {
                userArr = LhdVO.getVo().structUserList
            }
            if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.BREBG) {
                userArr = BrebgVO.getVo().structUserList
            }
            if (this.gameType == InstructionsEnum.GAME_TYPE_NAME.HHDZ) {
                userArr = HhdzVO.getVo().structUserList
            }
            if (this.effectType == 3) {
                this.panel.panenGroup.left = 0
                this.panel.panelBg.rotation = 0
                this.panel.closeBtn.icon = "userList_3"
                this.panel.closeBtn.left = 0
            }
            if (this.effectType == 4) {
                this.panel.panenGroup.right = 0
                this.panel.panelBg.rotation = 180
                this.panel.closeBtn.icon = "userList_5"
                this.panel.closeBtn.right = 0
            }

            //冒泡排序
            var h, j, temp, len = userArr.length;
            for (h = 0; h < len - 1; h++)
                for (j = 0; j < len - 1 - h; j++)
                    if (userArr[j]["lTakeMinScore"] < userArr[j + 1]["lTakeMinScore"]) {
                        temp = userArr[j];
                        userArr[j] = userArr[j + 1];
                        userArr[j + 1] = temp;
                    }


            var y = 0
            this.panel.data_group.removeChildren();
            for (var i = 0; i < userArr.length; i++) {
                var obj = userArr[i]
                var group = new eui.Group();
                group.y = y

                var bg = new eui.Image("home_21")
                bg.x = 21
                bg.y = 8
                group.addChild(bg)

                var touxiang = new eui.Image("special_" + obj["wFaceID"] % 15)
                touxiang.x = 24
                touxiang.y = 11
                touxiang.width = 88
                touxiang.height = 88
                group.addChild(touxiang)

                var idImg = new eui.Image("userList_1")
                idImg.x = 125
                idImg.y = 15
                group.addChild(idImg)
                var jinbiImg = new eui.Image("userList_2")
                jinbiImg.x = 125
                jinbiImg.y = 55
                group.addChild(jinbiImg)

                var id = new eui.Label(Global.cutString(obj["szCityName"]))
                id.x = 165
                id.y = 17
                id.size = 16
                group.addChild(id)

                var jinbi = new eui.Label(obj["lTakeMinScore"] / 100 + "")
                jinbi.x = 165
                jinbi.y = 65
                jinbi.size = 16
                group.addChild(jinbi)

                var fenge = new eui.Image("userList_4")
                fenge.x = 22
                fenge.y = 110
                fenge.width = 250
                group.addChild(fenge)

                this.panel.data_group.addChild(group)
                y += 114;
            }
        }

        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            if (this.effectType == 3) {
                this.closePanel(4);
            }
            if (this.effectType == 4) {
                this.closePanel(3);
            }

        }
    }
}